/**
 * Created by Administrator on 2017/5/22 0022.
 */
var person = JSON.parse(sessionStorage.getItem("person"));
if (person==''||person==null) {
    alert('请先登录')
    window.location.href='denglu.html';
} else{
    var userId = person.userid;
}
//我的消息列表
//        $.ajax({
//            type: "get",
//            url:"http://114.112.96.184:18080/view/myMsg",
//            data:{'userId':userId},
//            error: function(request) {
//                console.log("Connection error");
//            },
//            success: function(data) {
//                console.log(data)
//                var obj = data.result.content;
//                var htmls='';
//                for (var i = 0; i < obj.length;i++) {
//                   htmls+=  '<div class="panel panel-default">'+
//								 '<div class="panel-heading">'+
//								 '<h3 class="panel-title">系统消息 <small class="pull-right" style="margin-top: 10px;">'+ obj[i].createTime +'</small></h3>'+
//							'</div>'+
//							 '<div class="panel-body" style="padding-left: 0;padding-right: 0;padding-bottom: 30px;">'+
//								 '<p class="col-xs-10 col-xs-offset-1" style="background-color: #f2f2f2;padding: 20px;margin-top: 20px;" >'+obj[i].content+'</p>'+
//							 '</div>'+
//							 '</div>'+
//                    '</div>'
//                }
//                $('#myMsgList').html(htmls);
//            }
//        });

getTutorList({'userId':userId},1,10);
function getTutorList(objs,page,pageSize) {
    objs.index=page;
    objs.size=pageSize;
    $.ajax({
        type: "get",
        data:objs,
        url:"http://114.112.96.184:18080/view/myMsg",
        async: false,
        error: function(request) {
            console.log("Connection error");
        },
        success: function(data) {
            console.log(data);
            var pages=Math.ceil(data.result.totalElements/10);
            if(pages<=1){
                $("#pageTutor").hide();
            }else{
                $("#pageTutor").show();
            }
            var options= {
                bootstrapMajorVersion: 3,
                currentPage: page,    //当前页数
                numberOfPages: 10,
                totalPages: pages,    //所有数据可以显示的页数
                tooltipTitles: function () {
                },
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "prev":
                            return "←上一页";
                        case "next":
                            return "下一页→";
                        case "page":
                            return page;
                    }
                },
                shouldShowPage: function (type, page, current) {
                    switch (type) {
                        case "first":
                        case "last":
                            return false;
                        default:
                            return true;
                    }
                },
                onPageClicked: function (event, originalEvent, type, page) {
                    objs.index=page;
                    $.ajax({
                        type: "get",
                        data: {'userId': userId, 'index': page, 'size': pageSize},
                        url: "http://114.112.96.184:18080/view/myMsg",
                        async: false,
                        error: function (request) {
                            console.log("Connection error");
                        },
                        success: function (data) {
                            var obj = data.result.content;
                            var htmls='';
                            for (var i = 0; i < obj.length;i++) {
                                var content =obj[i].content;
                                var operating='<span type="delete">删除</span>';
                                if(obj.type==1){
                                    operating+=' <span type="yes">同意</span> <span type="no">拒绝</span> '
                                }
                                htmls+= '<dd>'+
                                    '<div class="row">'+
                                    '<div class="col-xs-3" style="padding-top:20px ;">'+obj[i].createTime+'</a></div>'+
                                    '<div class="col-xs-2">'+'系统消息'+'</div>'+
                                    '<div class="col-xs-4">'+ content+'</div>'+
                                    '<div class="col-xs-3 option" attentionId="'+obj[i].id+'">'+ operating +'</div>'+
                                    '</div>'+
                                    '</dd>'
                            }
                            $('#daoshi_list').html(htmls);

                        }
                    })


                }
            }

            if(data.result.totalElements>0){
                $("#pageTutor").bootstrapPaginator(options);
            }

            var obj = data.result.content;
            var htmls='';
            for (var i = 0; i < obj.length;i++) {
                var content =obj[i].content;
                var operating='<span type="delete">删除</span>';
                if(obj[i].type==1){
                    operating+=' <span type="yes">同意</span> <span type="no">拒绝</span> ';

                    var conentJoin = $.parseJSON(content).tutors;
                    var tutors = '';
                    for(var j=0;j<conentJoin.length;j++){
                        if(conentJoin[j].tutorId==userId){
                            continue;
                        }
                        tutors+='<a href="cyzfw_ckds.html?'+ conentJoin[j].tutorId +'">'+ conentJoin[j].realName+'</a>'+'、'
                    }
                    tutors=tutors.substring(0,tutors.length-1);
                    content='该项目团队希望您与 '+tutors+' 联合辅导项目';
                    //							content=
                }
                htmls+= '<dd>'+
                    '<div class="row">'+
                    '<div class="col-xs-3" style="padding-top:20px ;">'+obj[i].createTime+'</a></div>'+
                    '<div class="col-xs-2">'+'系统消息'+'</div>'+
                    '<div class="col-xs-4">'+ content+'</div>'+
                    '<div class="col-xs-3 option" attentionId="'+obj[i].id+'">'+ operating +'</div>'+
                    '</div>'+
                    '</dd>'
            }
            $('#daoshi_list').html(htmls);

        }
    });
}
$('.option').on('click','span',function () {
    var optionType = $(this).attr('type')
    var id = $(this).parent().attr('attentionId');
    var that = $(this);
    switch(optionType)
    {
        case 'delete':
            deleteMsg();
            break;
        case 'yes' :
            yesMsg();
            break;
        case 'no' :
            noMsg();
            break;
        default:
//					n 与 case 1 和 case 2 不同时执行的代码
    }

    function deleteMsg() {
        $.ajax({
            type: "delete",
            url:"http://114.112.96.184:18080/view/myMsg/"+id +"?"+'userId='+userId,
            async: false,
            error: function(request) {
                console.log("Connection error");
            },
            success: function(data) {
                console.log(data);
                if(data.code=='000'){
                    that.parent().parent().remove();
                }else {
                    alert(data.msg);
                }
            }
        });
    }
    function yesMsg() {
        $.ajax({
            type: "post",
            data:{'userId':userId,'type':1},
            url:"http://114.112.96.184:18080/tutor/agree/"+id ,
            async: false,
            error: function(request) {
                console.log("Connection error");
            },
            success: function(data) {
                console.log(data);
                if(data.code=='000'){
                    that.parent().parent().remove();
                }else {
                    alert(data.msg);
                }
            }
        });
    }
    function noMsg() {
        $.ajax({
            type: "post",
            data:{'userId':userId,'type':0},
            url:"http://114.112.96.184:18080/tutor/agree/"+id ,
            async: false,
            error: function(request) {
                console.log("Connection error");
            },
            success: function(data) {
                console.log(data);
                if(data.code=='000'){
                    that.parent().parent().remove();
                }else {
                    alert(data.msg);
                }
            }
        });
    }


})